<?php

class WeddingBigServiceAction extends MY_Controller {
	
	/**
	 * weddingBigService class construct function
	 */
	function WeddingBigServiceAction() {
		parent::My_Controller ( "wedding" );
	}
	
	/**
	 * weddingBigServiceAction edit init function
	 * @param unknown_type $id
	 */
	function edit_init($id = false) {
		$this->load->library ( 'form_validation' );
		$this->load->view ( 'wedding/m_wedding_service_edit' );
	}
	/**
	 * weddingBigServiceAction edit function
	 */
	function edit() {
		$this->load->library ( 'form_validation' );
		$this->form_validation->set_rules ( 'name', '套餐名称', 'trim|required|max_length[50]|xss_clean' );
		$this->form_validation->set_rules ( 'price', '价格', 'trim|required|numeric' );
		$this->form_validation->set_rules ( 'info', '简介', 'trim|required|max_length[5000]|xss_clean' );
		$this->form_validation->set_rules ( 'tag', '', 'callback__tag_check' );
		
		if ($this->form_validation->run () == FALSE) {
			$data['wedding'] = $this->_form_data();
			if ($this->input->post('tag')) {
				$data['tag'] = implode(" ", $this->input->post('tag'));
			}
			$this->load->view ( 'wedding/m_wedding_service_edit', $data);
		} else {
			$wedding = $this->_form_data();
			$wedding['category_id'] = 100;
			$wedding['wedding_id'] = $this->session->userdata ( 'wedding_id' );
			$wedding['tag'] =  implode(" ", $wedding['tag']);
			$wedding['create_time'] = time();
			$this->load->model('wedding/weddingservicemodel', 'weddingservicemodel', true);
			$this->weddingservicemodel->insert($wedding);
			$wedding_id = $this->weddingservicemodel->get_insert_id();
			redirect('wedding/WeddingBigServiceAction/preview/'.$wedding_id);
		}
	}
	
	/**
	 * weddiingBigServiceAction preview function
	 * @param unknown_type $id
	 */
	function preview($id) {
		$this->load->model('wedding/weddingservicemodel', 'weddingservicemodel', true);
		$data['wedding'] = $this->weddingservicemodel->get_one($id);
		$data['flag'] = $this->session->userdata ( 'flag' );
		$this->load->view ( 'wedding/m_wedding_service_preview', $data);
	}
	
	function _tag_check() {
		if (!$this->input->post('tag')) {
			$this->form_validation->set_message('_tag_check', '请选择服务项目');
			return false;
		} else {
			return true;
		}
	}
	/**
	 * wedding bing service upload image function
	 * httprequest ajax
	 */
	function img_upload() {
		//load model
		$this->load->model('wedding/weddingservicemodel', 'weddingservicemodel', true);
		//set ajax http head
		$this->_set_ajax_head ();
	 	$db_data = $this->weddingservicemodel->get_one($this->input->post('id'));
		$str_array = explode(' ', $db_data['img']);
		if ($this->session->userdata('flag')  == "0") {
			if (count($str_array) >= 3) {
				$json = array ();
				$json ['error_msg'] = "普通用户最多上传三张图片 VIP用户可以上传3张以上图片";
				echo json_encode ( $json );
				return;
			}	
		}
		if ($this->session->userdata('flag')  == "3") {
			if (count($str_array) >= 10) {
				$json = array ();
				$json ['error_msg'] = "VIP用户最多上传10张图片";
				echo json_encode ( $json );
				return;
			}	
		}
		
		//load my library common class
		$this->load->library ( 'common' );
		$file_name = $this->common->uuid ();
		//set upload path
		$file_path = $this->_get_wedding_path ();
		$config ['upload_path'] = $_SERVER ['DOCUMENT_ROOT'] . $file_path;
		$config ['allowed_types'] = 'gif|jpg|png';
		$config ['max_size'] = '200';
		$config ['max_width'] = '1024';
		$config ['max_height'] = '768';
		$config ['file_name'] = $file_name;
		$this->load->library ( 'upload', $config );
		//define a reponse josn object
		$json = array ();
		$json ['error_msg'] = "";
		$json ['img_path'] = "";
		//check is have error if have error put error msg into json array
		if (! $this->upload->do_upload ( 'img' )) {
			$error = array ('error' => $this->upload->display_errors () );
			foreach ( $error as $v ) {
				if (trim ( $v ) != "") {
					$json ['error_msg'] = $json ['error_msg'] . $v;
				}
			}
		} else {
			$info = $this->upload->data ();
			$json ['path'] = $file_path . $info ['orig_name'];
			$form_data = array();
			$form_data['id'] = $this->input->post('id');
			$form_data['img'] = $json ['path'];
		
	        $json ['path'] = $this->weddingservicemodel->update_img( $form_data );
	 	}
		echo json_encode ( $json );
	}
	
	function ajax_del_img() {
		$this->load->model('wedding/weddingservicemodel', 'weddingservicemodel', true);
		$id = $this->input->post('id');
		$path = $this->input->post('path');
		unlink ($_SERVER ['DOCUMENT_ROOT'] . $path);
		$this->weddingservicemodel->delete_img($path, $id);

	}
}

?>